package com.ruoyi.statistics.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.statistics.domain.GdArea;
import com.ruoyi.statistics.domain.GdType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.statistics.mapper.StatisticsGdDataMapper;
import com.ruoyi.statistics.domain.StatisticsGdData;
import com.ruoyi.statistics.service.IStatisticsGdDataService;

/**
 * 耕地Service业务层处理
 *
 * @author rww
 * @date 2023-06-06
 */
@Service
public class StatisticsGdDataServiceImpl implements IStatisticsGdDataService
{
    @Autowired
    private StatisticsGdDataMapper statisticsGdDataMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询耕地
     *
     * @param id 耕地主键
     * @return 耕地
     */
    @Override
    public StatisticsGdData selectStatisticsGdDataById(Long id)
    {
        return statisticsGdDataMapper.selectStatisticsGdDataById(id);
    }

    /**
     * 查询耕地列表
     *
     * @param statisticsGdData 耕地
     * @return 耕地
     */
    @Override
    public List<StatisticsGdData> selectStatisticsGdDataList(StatisticsGdData statisticsGdData)
    {
        return statisticsGdDataMapper.selectStatisticsGdDataList(statisticsGdData);
    }

    /**
     * 新增耕地
     *
     * @param statisticsGdData 耕地
     * @return 结果
     */
    @Override
    public int insertStatisticsGdData(StatisticsGdData statisticsGdData)
    {
        return statisticsGdDataMapper.insertStatisticsGdData(statisticsGdData);
    }

    /**
     * 修改耕地
     *
     * @param statisticsGdData 耕地
     * @return 结果
     */
    @Override
    public int updateStatisticsGdData(StatisticsGdData statisticsGdData)
    {
        return statisticsGdDataMapper.updateStatisticsGdData(statisticsGdData);
    }

    /**
     * 批量删除耕地
     *
     * @param ids 需要删除的耕地主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsGdDataByIds(Long[] ids)
    {
        return statisticsGdDataMapper.deleteStatisticsGdDataByIds(ids);
    }

    /**
     * 删除耕地信息
     *
     * @param id 耕地主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsGdDataById(Long id)
    {
        return statisticsGdDataMapper.deleteStatisticsGdDataById(id);
    }

    @Override
    public List<GdArea> selectStatisticsGdDistrict() {
        String key = "gd_district";
        List<GdArea> gdDistrict = redisCache.getCacheList(key);
        if(gdDistrict==null||gdDistrict.size()==0){
            List<StatisticsGdData> statisticsGdData = statisticsGdDataMapper.selectStatisticsGdDistrict();
            if(statisticsGdData!=null){
                gdDistrict = new ArrayList<>();
                List<GdArea> finalGdDistrict = gdDistrict;
                statisticsGdData.stream()
                        .collect(Collectors.groupingBy(StatisticsGdData::getProvince))
                        .forEach((province,cities)->{
                            GdArea p = new GdArea();
                            p.setName(province);
                            List<GdArea> cChildren = new ArrayList<>();
                            cities.stream().collect(Collectors.groupingBy(StatisticsGdData::getCity))
                            .forEach((city,districts)->{
                                GdArea c = new GdArea();
                                c.setName(city);
                                List<GdArea> dChildren = districts.stream().map(district->{
                                    GdArea d = new GdArea();
                                    d.setName(district.getDistrict());
                                    return d;
                                }).collect(Collectors.toList());
                                c.setChildren(dChildren);
                                cChildren.add(c);
                            });
                            p.setChildren(cChildren);
                            finalGdDistrict.add(p);
                        });
                redisCache.setCacheList(key,gdDistrict);
            }
        }
        return gdDistrict;
    }

    @Override
    public List<String> selectStatisticsGdType() {
        String key = "gd_types";
        List<String> gdTypes = redisCache.getCacheList(key);
        if(gdTypes==null){
            gdTypes = statisticsGdDataMapper.selectStatisticsGdType();
            redisCache.setCacheList(key,gdTypes);
        }
        return gdTypes;
    }

    @Override
    public List<GdType> statisticsGdType(StatisticsGdData statisticsGdDat) {
        return statisticsGdDataMapper.statisticsGdType(statisticsGdDat);
    }

    @Override
    public String importData(List<StatisticsGdData> statisticsGdData) {
        if(statisticsGdData==null||statisticsGdData.size()==0)return "导入数据为空";
        statisticsGdDataMapper.cleanData();
        statisticsGdData.stream().forEach(item->{
            statisticsGdDataMapper.insertStatisticsGdData(item);
        });
        String key = "gd_district";
        String key2 = "gd_types";
        redisCache.deleteObject(key);
        redisCache.deleteObject(key2);
        return "导入成功";
    }
}
